home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / test / test18.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  32KB  |  1,351 lines

  1. /* test 18 */
  2.  
  3. /* Comment on usage and program: ark!/mnt/rene/prac/os/unix/comment.changes */
  4.  
  5. /* "const.h", created by Rene Montsma and Menno Wilcke */
  6.  
  7.  
  8. #include <sys/types.h>        /* needed in struct stat */
  9. #include <sys/stat.h>        /* struct stat */
  10. #include <errno.h>        /* the error-numbers */
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13. #include <stdio.h>
  14.  
  15. #define NOCRASH 1        /* test11(), 2nd pipe */
  16. #define PDPNOHANG  1        /* test03(), write_standards() */
  17.  
  18.  
  19. #define USER_ID   12
  20. #define GROUP_ID   1
  21. #define FF        3        /* first free filedes. */
  22. #define USER      1        /* uid */
  23. #define GROUP     0        /* gid */
  24.  
  25. #define ARSIZE   256        /* array size */
  26. #define PIPESIZE 3584        /* maximum number of bytes to be * written on
  27.              * pipe               */
  28.  
  29. #define MAXOPEN  17        /* maximum number of extra open files */
  30.  
  31. #define MAXLINK 0177        /* maximum number of links per file */
  32.  
  33. #define MASK    0777        /* selects lower nine bits */
  34.  
  35. #define READ_EOF 0        /* returned by read-call at eof */
  36.  
  37. #define OK      0
  38. #define FAIL   -1
  39.  
  40. #define R       0        /* read (open-call) */
  41. #define W       1        /* write (open-call) */
  42. #define RW      2        /* read & write (open-call) */
  43.  
  44. #define RWX     7        /* read & write & execute (mode) */
  45.  
  46. #define NIL     ""
  47. #define UMASK   "umask"
  48. #define CREAT   "creat"
  49. #define WRITE   "write"
  50. #define READ    "read"
  51. #define OPEN    "open"
  52. #define CLOSE   "close"
  53. #define LSEEK   "lseek"
  54. #define ACCESS  "access"
  55. #define CHDIR   "chdir"
  56. #define CHMOD   "chmod"
  57. #define LINK    "link"
  58. #define UNLINK  "unlink"
  59. #define PIPE    "pipe"
  60. #define STAT    "stat"
  61. #define FSTAT   "fstat"
  62. #define DUP     "dup"
  63. #define UTIME   "utime"
  64.  
  65. extern int errno;        /* the error-number */
  66. int errct;
  67.  
  68. long lseek();
  69.  
  70. char *file[];
  71. char *fnames[];
  72. char *dir[];
  73.  
  74. /* "decl.c", created by Rene Montsma and Menno Wilcke */
  75.  
  76. /* Used in open_alot, close_alot */
  77. char *file[20] = {"f0", "f1", "f2", "f3", "f4", "f5", "f6",
  78.       "f7", "f8", "f9", "f10", "f11", "f12", "f13",
  79.       "f14", "f15", "f16", "f17", "f18", "f19"}, *fnames[8] = {"---", "--x", "-w-", "-wx", "r--",
  80.                                    "r-x", "rw-", "rwx"}, *dir[8] = {"d---", "d--x", "d-w-", "d-wx", "dr--", "dr-x",
  81.                             "drw-", "drwx"};
  82.  /* Needed for easy creating and deleting of directories */
  83.  
  84. /* "test.c", created by Rene Montsma and Menno Wilcke */
  85.  
  86.  
  87. /*****************************************************************************
  88.  *                              TEST                                         *
  89.  ****************************************************************************/
  90. main()
  91. {
  92.   int n;
  93.  
  94.   if (fork()) {
  95.     printf("Test 18 ");
  96.     fflush(stdout);        /* have to flush for child's benefit */
  97.  
  98.     wait(&n);
  99.     clean_up_the_mess();
  100.     if (errct == 0)
  101.         printf("ok\n");
  102.     else
  103.         printf(" %d errors\n", errct);
  104.     exit(0);
  105.   } else {
  106.     test();
  107.     exit(0);
  108.   }
  109. }
  110.  
  111.  
  112.  
  113. test()
  114. {
  115.   int n;
  116.   umask(0);            /* not honest, but i always forget */
  117.  
  118.   test01();
  119.   make_and_fill_dirs();
  120.   test02();
  121.   test03();
  122.   test04();
  123.   test05();
  124.   test06();
  125.   test07();
  126.   umask(022);
  127. }                /* test */
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134. /* "t1.c" created by Rene Montsma and Menno Wilcke */
  135.  
  136. /*****************************************************************************
  137.  *                              test UMASK                                   *
  138.  ****************************************************************************/
  139. test01()
  140. {
  141.   int oldvalue, newvalue, tempvalue;
  142.   int nr;
  143.  
  144.   if ((oldvalue = umask(0777)) != 0) err(0, UMASK, NIL);
  145.  
  146.   /* Special test: only the lower 9 bits (protection bits) may part- *
  147.    * icipate. ~0777 means: 111 000 000 000. Giving this to umask must*
  148.    * not change any value.                                           */
  149.  
  150.   if ((newvalue = umask(~0777)) != 0777) err(1, UMASK, "illegal");
  151.   if (oldvalue == newvalue) err(11, UMASK, "not change mask");
  152.  
  153.   if ((tempvalue = umask(0)) != 0) err(2, UMASK, "values");
  154.  
  155.  
  156.   /* Now test all possible modes of umask on a file */
  157.   for (newvalue = MASK; newvalue >= 0; newvalue -= 0111) {
  158.     tempvalue = umask(newvalue);
  159.     if (tempvalue != oldvalue) {
  160.         err(1, UMASK, "illegal");
  161.         break;        /* no use trying more */
  162.     } else if ((nr = creat("file01", 0777)) < 0)
  163.         err(5, CREAT, "'file01'");
  164.     else {
  165.         try_close(nr, "'file01'");
  166.         if (get_mode("file01") != (MASK & ~newvalue))
  167.             err(7, UMASK, "mode computed");
  168.         try_unlink("file01");
  169.     }
  170.     oldvalue = newvalue;
  171.   }
  172.  
  173.   /* The loop has terminated with umask(0) */
  174.   if ((tempvalue = umask(0)) != 0)
  175.     err(7, UMASK, "umask may influence rest of tests!");
  176. }                /* test01 */
  177.  
  178.  
  179.  
  180. /*****************************************************************************
  181.  *                              test CREAT                                   *
  182.  ****************************************************************************/
  183. test02()
  184. {
  185.   int n, n1, mode;
  186.   char a[ARSIZE], b[ARSIZE];
  187.   struct stat stbf1;
  188.  
  189.   mode = 0;
  190.   /* Create twenty files, check filedes */
  191.   for (n = 0; n < MAXOPEN; n++) {
  192.     if (creat(file[n], mode) != FF + n)
  193.         err(13, CREAT, file[n]);
  194.     else {
  195.         if (get_mode(file[n]) != mode)
  196.             err(7, CREAT, "mode set while creating many files");
  197.  
  198.         /* Change  mode of file to standard mode, we want to *
  199.          * use a lot (20) of files to be opened later, see   *
  200.          * open_alot(), close_alot().                        */
  201.         if (chmod(file[n], 0700) != OK) err(5, CHMOD, file[n]);
  202.  
  203.     }
  204.     mode = (mode + 0100) % 01000;
  205.   }
  206.  
  207.   /* Already twenty files opened; opening another has to fail */
  208.   if (creat("file02", 0777) != FAIL)
  209.     err(9, CREAT, "created");
  210.   else
  211.     check(CREAT, EMFILE);
  212.  
  213.   /* Close all files: seems blunt, but it isn't because we've  *
  214.    * checked all fd's already                                  */
  215.   if ((n = close_alot(MAXOPEN)) < MAXOPEN) err(5, CLOSE, "MAXOPEN files");
  216.  
  217.   /* Creat 1 file twice; check */
  218.   if ((n = creat("file02", 0777)) < 0)
  219.     err(5, CREAT, "'file02'");
  220.   else {
  221.     init_array(a);
  222.     if (write(n, a, ARSIZE) != ARSIZE) err(1, WRITE, "bad");
  223.  
  224.     if ((n1 = creat("file02", 0755)) < 0)    /* receate 'file02' */
  225.         err(5, CREAT, "'file02' (2nd time)");
  226.     else {
  227.         /* Fd should be at the top after recreation */
  228.         if (lseek(n1, 0L, SEEK_END) != 0)
  229.             err(11, CREAT, "not truncate file by recreation");
  230.         else {
  231.             /* Try to write on recreated file */
  232.             clear_array(b);
  233.  
  234.             if (lseek(n1, 0L, SEEK_SET) != 0)
  235.                 err(5, LSEEK, "to top of 2nd fd 'file02'");
  236.             if (write(n1, a, ARSIZE) != ARSIZE)
  237.                 err(1, WRITE, "(2) bad");
  238.  
  239.             /* In order to read we've to close and open again */
  240.             try_close(n1, "'file02'  (2nd creation)");
  241.             if ((n1 = open("file02", RW)) < 0)
  242.                 err(5, OPEN, "'file02'  (2nd recreation)");
  243.  
  244.             /* Continue */
  245.             if (lseek(n1, 0L, SEEK_SET) != 0)
  246.                 err(5, LSEEK, "to top 'file02'(2nd fd) (2)");
  247.             if (read(n1, b, ARSIZE) != ARSIZE)
  248.                 err(1, READ, "wrong");
  249.  
  250.             if (comp_array(a, b, ARSIZE) != OK) err(11, CREAT,
  251.                     "not really truncate file by recreation");
  252.         }
  253.         if (get_mode("file02") != 0777)
  254.             err(11, CREAT, "not maintain mode by recreation");
  255.         try_close(n1, "recreated 'file02'");
  256.  
  257.     }
  258.     Remove(n, "file02");
  259.   }
  260.  
  261.   /* Give 'creat' wrong input: dir not searchable */
  262.   if (creat("drw-/file02", 0777) != FAIL)
  263.     err(4, CREAT, "'drw-'");
  264.   else
  265.     check(CREAT, EACCES);
  266.  
  267.   /* Dir not writable */
  268.   if (creat("dr-x/file02", 0777) != FAIL)
  269.     err(12, CREAT, "'dr-x/file02'");
  270.   else
  271.     check(CREAT, EACCES);
  272.  
  273.   /* File not writable */
  274.   if (creat("drwx/r-x", 0777) != FAIL)
  275.     err(11, CREAT, "recreate non-writable file");
  276.   else
  277.     check(CREAT, EACCES);
  278.  
  279.   /* Try to creat a dir */
  280.   if ((n = creat("dir", 040777)) != FAIL) {
  281.     if (fstat(n, &stbf1) != OK)
  282.         err(5, FSTAT, "'dir'");
  283.     else if (stbf1.st_mode != 0100777)
  284.         err(11, CREAT, "'creat' a new directory");
  285.     Remove(n, "dir");
  286.   }
  287.  
  288.   /* We don't consider it to be a bug when creat * does not accept
  289.    * tricky modes                */
  290.  
  291.   /* File is an existing dir */
  292.   if (creat("drwx", 0777) != FAIL)
  293.     err(11, CREAT, "create an existing dir!");
  294.   else
  295.     check(CREAT, EISDIR);
  296. }                /* test02 */
  297.  
  298.  
  299.  
  300.  
  301.  
  302. /*****************************************************************************
  303.  *                              test WRITE                                   *
  304.  ****************************************************************************/
  305. test03()
  306. {
  307.   int n, n1;
  308.   int fd[2];
  309.   char a[ARSIZE];
  310.  
  311.   init_array(a);
  312.  
  313.   /*